//
//  RequestManage.m
//  MUBATE
//
//  Created by sunbin on 2017/2/21.
//  Copyright © 2017年 Quanli. All rights reserved.
//

#import "RequestManage.h"

@interface RequestManage()


@end

@implementation RequestManage

+ (void)requestApi:(NSArray *)api params:(NSArray *)params model:(NSArray *)model
    responseCache :(void (^)(BOOL isHaveCash , NSInteger index ,NSDictionary *cashDic ,NSMutableArray *cashAry))cache
    refreshHeadler:(void (^)(int index))refresh
           success:(void (^)(NSDictionary * succesSults,NSMutableArray *dataAry , NSString* successRequestmsg ,NSInteger index))success
           failure:(void (^)(NSDictionary * failedSults ,NSMutableArray *arySults, responseState resMake, NSString* RequestError , NSInteger index))failed
           showHub:(void (^)(bool isShowHub ,NSInteger index))showHub{
    
    // 查看缓存
    
    __block NSMutableArray *checkCashAry = [NSMutableArray array];
    
    // 保存数组 与字典 存在问题就当数据为空时的操作
    __block NSMutableArray *modelToArys = [NSMutableArray array];
    __block NSMutableArray *modelToDics = [NSMutableArray array];

    
    
    dispatch_queue_t serialQueue=dispatch_queue_create("myThreadQueue1", DISPATCH_QUEUE_SERIAL);//注意queue对象不是指针类型
    for (int i = 0; i < api.count; i++) {
        //        //异步执行队列任务
        //        dispatch_sync(serialQueue, ^{
        //
        //        });
        //        dispatch_async
        dispatch_sync(serialQueue, ^{
            [ZL_NetworkHelper POST:api[i] parameters:params[i] responseCache:^(id responseCache) {
                // 注意 没有缓存responseCache为nil
                // 响应体数据
                NSDictionary *dic = (NSDictionary *)responseCache;
                
                NSInteger count = [(NSDictionary*)responseCache count];
                BOOL isCash = count!=0?YES:NO;//查看是否没有缓存 没有缓存就没有数据
                NSString *str = isCash==YES?@"1":@"0";
                [checkCashAry addObject:str];
                
                
                
                    // 获取model
                    NSString *modelStr = model[i];
#pragma make data数据类型不确定 1数组 2字典 之后转数据与model转换
                    // 获取data
                    // 1数组
                    NSArray *dataAry = [dic objectForKey:@"data"];
                    NSMutableArray *data =  [NSClassFromString(modelStr) mj_objectArrayWithKeyValuesArray:dataAry];
                    
                    // 2字典
                    // 简单的字典 -> 模型完后 在模型 -> 字典
                    NSDictionary *modelToDic = (NSDictionary *)[[NSClassFromString(modelStr) mj_objectWithKeyValues:[dic objectForKey:@"data"]] mj_keyValues];
                
                // 判断添加数组 字典元素是否为空
                [modelToArys addObject:[data count] !=0?data:@"0"];
                [modelToDics addObject:[modelToDic count] !=0?modelToDic:@"0"];

                
                cache(isCash ,i ,modelToDic ,data);//有缓存数据 在调用此方法后 就结束return
                
            } success:^(id responseObject) {
                
#pragma make 成功得到网络返回结果 1有数据（数据与model转换）  2无数据返回错误
                NSDictionary *dic = (NSDictionary *)responseObject;
                NSString *res = IF_NULL_TO_STRING([dic objectForKey:@"res"]);
                NSString *msg = IF_NULL_TO_STRING([dic objectForKey:@"msg"]);
                if ([res isEqualToString:@"1"]) {
                    // 1有数据
                    // 获取model
                    NSString *modelStr = model[i];
                    // 数组
                    NSMutableArray *data =  [NSClassFromString(modelStr) mj_objectArrayWithKeyValuesArray:[dic objectForKey:@"data"]];
                    // 简单的字典 -> 模型完后 在模型 -> 字典
                    NSDictionary *modelToDic = (NSDictionary *)[[NSClassFromString(modelStr) mj_objectWithKeyValues:[dic objectForKey:@"data"]] mj_keyValues];
                    success(modelToDic ,data ,msg ,i);
                    
                    
                }else{
                    // 2返回错误
                    failed([[modelToDics objectAtIndex:i] isKindOfClass:[NSString class]]?nil:[modelToDics objectAtIndex:i] ,[[modelToArys objectAtIndex:i] isKindOfClass:[NSString class]]?nil:[modelToArys objectAtIndex:i],Error ,msg ,i);
                    NSString *index = [checkCashAry objectAtIndex:i];
                    
                    showHub(![index isEqualToString:@"1"]?YES:NO ,i);
                    
                }
                refresh(i);
            } failure:^(NSError *error) {
                
                failed([[modelToDics objectAtIndex:i] isKindOfClass:[NSString class]]?nil:[modelToDics objectAtIndex:i] ,[[modelToArys objectAtIndex:i] isKindOfClass:[NSString class]]?nil:[modelToArys objectAtIndex:i],Failure ,error.localizedDescription ,i);

                NSString *index = [checkCashAry objectAtIndex:i];
                showHub(![index isEqualToString:@"1"]?YES:NO ,i);
            }];
            });}}





@end
